import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop) 
         
        toolBar =QToolBar()        

        toolBar.addWidget(QLabel('Item:'))
        self.lineEdit = QLineEdit()
        toolBar.addWidget(self.lineEdit)

        btn = QPushButton('Add')
        btn.clicked.connect(self.OnAddClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Insert')
        btn.clicked.connect(self.OnInsertClicked)
        toolBar.addWidget(btn)        

        btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDelClicked)
        toolBar.addWidget(btn)
        
        btn = QPushButton('Update')
        btn.clicked.connect(self.OnUpdateClicked)
        toolBar.addWidget(btn) 
        layout.addWidget(toolBar)

        group =QGroupBox('Default ComboBox')
        groupLayout=QVBoxLayout()        
        self.combox = QComboBox()
        self.combox.currentTextChanged.connect(self.CurrentTextChanged)
        groupLayout.addWidget(self.combox)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        group =QGroupBox('Editable ComboBox')
        groupLayout=QVBoxLayout()        
        self.comboxEdit = QComboBox()
        self.comboxEditData=[]
        self.comboxEdit.setEditable(True)
        '''self.completer = QCompleter()   
        self.completer.setFilterMode(Qt.MatchFlag.MatchContains)
        self.completer.setCompletionMode(QCompleter.CompletionMode.PopupCompletion)   
        self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)  
        self.comboxEdit.setEditable(True)
        self.comboxEdit.setCompleter(self.completer)  '''      
        self.comboxEdit.editTextChanged.connect(self.EditTextChanged)
        groupLayout.addWidget(self.comboxEdit)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        group =QGroupBox('ItemModel ComboBox')
        groupLayout=QVBoxLayout()        
        self.comboxItemModel = QComboBox()
        self.listViewData = QStandardItemModel()    
        self.comboxItemModel.setModel(self.listViewData)
        self.comboxItemModel.editTextChanged.connect(self.EditTextChanged)
        groupLayout.addWidget(self.comboxItemModel)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        self.setLayout(layout) 
    def OnAddClicked(self):        
        text=self.lineEdit.text()
        self.combox.addItem(text)        
        #comboxEdit设置自动补全
        self.comboxEdit.addItem(text)
        self.comboxEditData.append(text)
        self.completer = QCompleter(self.comboxEditData)
        self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)          
        self.comboxEdit.setCompleter(self.completer)
        #comboxItemModel
        self.listViewData.appendRow(QStandardItem(text))               
        self.parent.OnShowMessage(f'Add Item: {text}')
    def OnInsertClicked(self):
        text=self.lineEdit.text()
        index = self.combox.currentIndex()
        if index==-1:
            index=0
        self.combox.insertItem(index,text)
        #comboxEdit
        index=self.comboxEdit.currentIndex()
        if index==-1:
            index=0
        self.comboxEdit.insertItem(index,text)        
        self.comboxEditData.insert(index,text)
        self.completer = QCompleter(self.comboxEditData)
        self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)          
        self.comboxEdit.setCompleter(self.completer)
        #comboxItemModel
        index = self.comboxItemModel.currentIndex()
        if index==-1:
            index=0
        self.listViewData.insertRow(index,QStandardItem(text))
        self.parent.OnShowMessage(f'Insert Item: {text}')
    def OnDelClicked(self):
        index = self.combox.currentIndex()
        if index!=-1:
            self.parent.OnShowMessage(f'Delete Item: {self.combox.currentText()}')
            self.combox.removeItem(index)
        #comboxEdit
        index =self.comboxEdit.currentIndex()
        if index!=-1:
            text =  self.comboxEdit.currentText()          
            self.parent.OnShowMessage(f'Delete Item: {text}')
            
            self.comboxEdit.removeItem(index)
            self.comboxEditData.remove(text)
            self.completer = QCompleter(self.comboxEditData)
            self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)          
            self.comboxEdit.setCompleter(self.completer)
        #comboxItemModel
        index = self.comboxItemModel.currentIndex()
        if index!=-1:            
            self.parent.OnShowMessage(f'Delete Item: {self.comboxItemModel.currentText()}')
            self.listViewData.removeRow(index)
    def OnUpdateClicked(self):
        new = self.lineEdit.text()
        index = self.combox.currentIndex()
        if index!=-1:
            old = self.combox.currentText()        
            self.combox.setItemText(index,new)
            self.parent.OnShowMessage(f"Update Item from '{old}' to '{new}'")
        #comboxEdit
        index = self.comboxEdit.currentIndex()
        if index!=-1:
            old = self.comboxEdit.currentText()        
            self.comboxEdit.setItemText(index,new)
            self.comboxEditData[index]=new
            self.completer = QCompleter(self.comboxEditData)
            self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)          
            self.comboxEdit.setCompleter(self.completer)
            self.parent.OnShowMessage(f"Update Item from '{old}' to '{new}'")
        #comboxItemModel
        index = self.comboxItemModel.currentIndex()
        if index!=-1:
            old = self.comboxItemModel.currentText() 
            modelIndex= self.listViewData.index(index,0) 
            self.listViewData.itemFromIndex(modelIndex).setText(new)            
            self.parent.OnShowMessage(f"Update Item from '{old}' to '{new}'")
    def CurrentTextChanged(self,text):
        self.parent.OnShowMessage(f'Current Item: {text}')
    def EditTextChanged(self,text):
        self.parent.OnShowMessage(f"Edit Text:{text}")
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet